// if (location.href.indexOf('sk=') < 0 && location.href.indexOf('detail.1688.com') > 0) {
// 	let new_url = location.href.replaceAll(/html?.*/g, 'html?sk=consign');
// 	location.replace(new_url);
// }
// console.log('inject');
const $gen_btn = $('<button class="load">加载</button>');
$("body").append($gen_btn);
$gen_btn.click(() => {
	// $.get('https://g.alicdn.com/channel-fe/channel-toolbox/1.0.16/index.js',(e)=>{
	// 	console.log(e);
	// });
	$product_link = $('.co_rows_area');
	$product_link.each((i, item) => {
		// console.log(i, $(item).attr('href'));
		let $getInfo_btn = $(item).find('.getInfo');
		if ($getInfo_btn.length == 0) {
			$getInfo_btn = $('<button class="getInfo">获取信息</button>');
			$getInfo_btn.click(() => {
				const $a = $(item).parent().find('.zzb_item_checbox');
				// let token = document.cookie.match(/_m_h5_tk=.*?_/)[0].replace(/_m_h5_tk=|_/g,
				// 	'');
				// console.log(token);
				let link = $a.attr('itemurl') + "?sk=consign";
				console.log(link);
				chrome.runtime.sendMessage({
					link
				}, function(response) {
					console.log(response.sku);
					let modal_content =
						`${response.skuPriceScaleOriginal} ${response.postFree}<br>`;
					for (let i of Object.keys(response.sku)) {
						modal_content +=
							`${i.replace('&gt',' ')} ￥${response.sku[i].price}<br>`
					};
					console.log(modal_content);
					const mModal1 = new mModal({
						content: modal_content,
						confirm: function() {
							mModal1.close();
						}
					});
					mModal1.renderDom();
					$getInfo_btn.text(response.skuPriceScaleOriginal + response
						.postFree);
				});
			});
			$(item).append($getInfo_btn);
		}
	});
	const $saleNum = $('span:contains("月 代 销: ")+.content');
	// let i=0,j=0,k=0;
	$saleNum.each((u, it) => {
		let num = parseInt($(it).text().match(/\d*/));
		const parent = $(it).parents(
			'a.ocms-fusion-1688-pc-pc-ad-common-offer-2024,a.search-offer-wrapper,div.normalcommon-offer-card'
		);
		// console.log(parent);
		let classText = parent.attr('class');
		// console.log(classText);
		// console.log(num);
		// console.log(classText.indexOf('high'));
		if (classText.indexOf('high') == -1) {
			if (!isNaN(num)) {
				if (num > 900) {
					parent.addClass('many');
				} else if (num > 500) {
					parent.addClass('medium');
				} else if (num > 100) {
					parent.addClass('less');
				}
			}
		}
	});
	const $offerDelivery48hRate = $('span:contains("48H发货率: ")+.content');
	$offerDelivery48hRate.each((u, it) => {
		let num = parseInt($(it).text().match(/\d*/));
		const parent = $(it).parents(
			'a.ocms-fusion-1688-pc-pc-ad-common-offer-2024,a.search-offer-wrapper,div.normalcommon-offer-card'
		);
		// console.log(parent);
		let classText = parent.attr('class');
		if (!isNaN(num)) {
			if (num > 95) {
				parent.css('background', 'wheat');
			} else if (num > 90) {
				parent.css('background', 'white');
			} else {
				parent.css('background', 'black');
			}
		}
	});
	alert(`>900:${$('.many').length},>500:${$('.medium').length},>100:${$('.less').length}`);
	// chrome.runtime.sendMessage({
	// 	link: 'www.baidu.com'
	// }, function(response) {
	// 	console.log(response);
	// });
});
// $(window).scroll(function() {
// 	console.log("窗口正在滚动...");
// 	$gen_btn.click();
// 	// 这里你可以添加更多的逻辑，比如检查滚动位置  
// });
// setInterval(()=>{
// 	// console.log('成功');
// 	$gen_btn.click();
// },2000);
// chrome.runtime.sendMessage({
// 	link: 'https://www.baidu.com'
// }, function(response) {
// 	console.log(response);
// });
// chrome.runtime.sendMessage({link:'https://www.baidu.com'}, function(response) {  
//     console.log('Received response: ', response);  
// });  